package org.dromara.demo.controller.redis;

import lombok.RequiredArgsConstructor;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Redis bitmap 数据类型做用户签到,统计指定用户一个月或者一年之中的登陆天数等等
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/redis/pubsub")
public class RedisBitmapController {


    /**
     * 用户签到 : 用户每天登陆一次,就给当天对应的bit设置为1
     * @return : R<Boolean>
     */
    public Boolean singIn(){
         //获取当前登入用户
        Long userId = LoginHelper.getLoginUser().getUserId();
        //获取当前日期
        LocalDateTime now = LocalDateTime.now();
        //拼接Redis的key
        String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyy-MM"));
        String key = "USER_SING_"+userId+keySuffix;
         //获取当前天是本月第几天
        int dayOfMonth = now.getDayOfMonth();
        //数据写入热点
        RedisUtils.getClient().getBitSet(key).set(dayOfMonth-1,true);
        return Boolean.TRUE;
    }

}
